<?php

namespace app\common\model;

use think\Db;
use think\Model;

/**
 * 打卡
 */
class Clock extends Model
{

    // 表名,不含前缀
    protected $name = 'clock';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    // 追加属性
    protected $append = [
        'item',
    ];


    //关联用户
    public function user()
    {
        return $this->hasOne(User::class, 'id', 'user_id')->field('id,nickname,avatar');
    }

    public function getItemAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['id']) ? $data['id'] : '');
        if (!$value){
            return [null,null];
        }
        $item[0] = ClockLog::where('clock_id', $value)->where('type',1)->find();
        $item[1] = ClockLog::where('clock_id', $value)->where('type',2)->find();
        return $item;
    }

    //打卡
    public static function onClock($params)
    {
        $user = User::info();
        extract($params);
        $row = Db::transaction(function () use (
            $user,
            $type,
            $image,
            $address
        ) {
            //获取今日打卡
            $day_clock = self::where(['user_id'=>$user->id,'date'=>date('Y-m-d')])->find();
            //今天第一次打卡,添加一条今日打卡数据
            if (!$day_clock){
                $day_clock = self::create([
                    'user_id'       => $user->id,
                    'nickname'      => $user->nickname,
                    'date'          => date('Y-m-d'),
                ]);
            }
            //当日上班卡
            $clock_1 = ClockLog::getUserTypeLog($user->id,$day_clock->id,1);
            //当日下班卡
            $clock_2 = ClockLog::getUserTypeLog($user->id,$day_clock->id,2);

            //如果上班卡不存在 且 现在是上午，则执行打上班卡
            if (!$clock_1 && $type == 1){
                return ClockLog::addClockLog($user->id,$day_clock->id,1,$image,$address);
            }

            //如果上班卡已存在，下班卡不存在，则随时可以打下班卡
            if ($clock_1 && !$clock_2){
                return ClockLog::addClockLog($user->id,$day_clock->id,2,$image,$address);
            }

            //如果上班卡不存在,下班卡也不存在，但是现在是下午，则可以打下班卡
            if (!$clock_1 && !$clock_2 && $type == 2){
                return ClockLog::addClockLog($user->id,$day_clock->id,2,$image,$address);
            }

            //如果存在打卡信息，则说明已经打过卡了
            if (($clock_1 && $clock_2) || ($type == 2 && $clock_2)){
                new \app\common\exception\Exception('您已经打过卡了');
            }
        });
        return $row;
    }


    //打卡记录
    public static function getClockLogs($params)
    {
        $user = User::info();
        extract($params);
        $where['user_id'] = $user->id;
        if (isset($starttime) && $starttime && isset($endtime) && $endtime) {
            $endtimeNext = date('Y-m-d',strtotime($endtime. '+1 day'));
            $where['date'] = ['between', [$starttime, $endtimeNext]];
        }
        return self::with('user')->where($where)->order('createtime desc')->paginate(10);
    }

    //用户当日打卡
    public static function getUserDay()
    {
        $user = User::info();
        $row = self::where('user_id',$user->id)->whereTime('createtime', 'today')->find();
        if (!$row) {
            return 0;
        }
        //当前打卡内容
        $flag = isMorningAfternoon();
        $log = ClockLog::where(['clock_id'=>$row->id,'type'=>$flag])->find();
        if (!$log) {
            return 0;
        }
        return 1;
    }

}
